package com.dacool.view;

import static com.dacool.util.ViewUtils.rp;

import java.lang.ref.WeakReference;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.RelativeLayout;

import com.dacool.util.ViewUtils;

public class TitleBar extends RelativeLayout {

	private static final int ID_FIRSTLEFT = 10086;
	private static final int ID_SECONDLEFT = 10010;
	private static final int ID_RIGHT = 10000;

	public TitleBar(Context context) {
		this(context, null);

	}

	public TitleBar(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public View getRightButton() {
		return rightButtonItem;
	}

	public View getLeftButton() {
		return leftButtonItem;
	}

	public View getTitleView() {
		return titleView;
	}

	public void setLeftButton(TitleItem item) {
		if (item != null && leftButtonItem == item.view)
			return;
		if (leftButtonItem != null) {
			this.removeView(leftButtonItem);
		}
		leftButtonItem = item != null ? item.view : null;
		if (leftButtonItem != null) {
			// leftButtonItem.setOnClickListener(new OnClickListener() {
			//
			// @Override
			// public void onClick(View v) {
			// // TODO Auto-generated method stub
			// if (delegate != null && delegate.get() != null) {
			// delegate.get().didTapLeftButton();
			// }
			// }
			// });
			leftButtonItem.setId(ID_FIRSTLEFT);
			this.addView(leftButtonItem, getLeftItemParams(item));
		}
	}

	public void setSecondLeftButton(TitleItem item) {
		if (item != null && secondLeftButtonItem == item.view)
			return;
		if (secondLeftButtonItem != null) {
			this.removeView(secondLeftButtonItem);
		}
		secondLeftButtonItem = item != null ? item.view : null;
		if (secondLeftButtonItem != null) {
			// leftButtonItem.setOnClickListener(new OnClickListener() {
			//
			// @Override
			// public void onClick(View v) {
			// // TODO Auto-generated method stub
			// if (delegate != null && delegate.get() != null) {
			// delegate.get().didTapLeftButton();
			// }
			// }
			// });
			secondLeftButtonItem.setId(ID_SECONDLEFT);
			this.addView(secondLeftButtonItem, getSecondLeftItemParams(item));
		}
	}

	public void setRightButtonItem(TitleItem item) {
		if (item != null && rightButtonItem == item.view)
			return;
		if (rightButtonItem != null) {
			this.removeView(rightButtonItem);
		}
		rightButtonItem = item != null ? item.view : null;
		if (rightButtonItem == null)
			return;
		this.addView(rightButtonItem, getRightItemParams(item));
	}

	public void setTitleView(TitleItem item) {
		if (item != null && titleView == item.view)
			return;

		if (titleView != null) {
			ViewUtils.removeFromSuperView(titleView);
		}
		titleView = item != null ? item.view : null;

		if (titleView != null) {
			titleView.setId(ID_SECONDLEFT);
			this.addView(titleView, getTitleItemParams(item));
		}
	}

	private WeakReference<TitleBarDelegate> delegate;
	private View leftButtonItem;
	private View secondLeftButtonItem;
	private View titleView;
	private View rightButtonItem;

	public static interface TitleBarDelegate {
		public void didTapLeftButton();

		public void didTapRightButton();
	}

	public void setDelegate(TitleBarDelegate delegate) {
		this.delegate = new WeakReference<TitleBar.TitleBarDelegate>(delegate);
	}

	private LayoutParams getLeftItemParams(TitleItem item) {
		LayoutParams lp;
		if (item.width <= 0 && item.height <= 0)
			lp = new LayoutParams((int) rp(40), (int) rp(40));
		else
			lp = new LayoutParams(item.width, item.height);
		lp.leftMargin = (int) rp(8);
		lp.addRule(RelativeLayout.CENTER_VERTICAL);
		return lp;
	}

	private LayoutParams getSecondLeftItemParams(TitleItem item) {
		LayoutParams lp;
		if (item.width <= 0 && item.height <= 0)
			lp = new LayoutParams((int) rp(40), (int) rp(40));
		else
			lp = new LayoutParams(item.width, item.height);
		lp.leftMargin = (int) rp(5);
		if (leftButtonItem != null)
			lp.addRule(RelativeLayout.RIGHT_OF, ID_FIRSTLEFT);
		lp.alignWithParent = true;
		lp.addRule(RelativeLayout.CENTER_VERTICAL);
		return lp;
	}

	private LayoutParams getRightItemParams(TitleItem item) {
		LayoutParams lp;
		if (item.width <= 0 && item.height <= 0)
			lp = new LayoutParams((int) rp(40), (int) rp(40));
		else
			lp = new LayoutParams(item.width, item.height);
		lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
		lp.rightMargin = (int) rp(5);
		lp.addRule(RelativeLayout.CENTER_VERTICAL);
		return lp;
	}

	private LayoutParams getTitleItemParams(TitleItem item) {
		LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT);
		if (item.width > 0 || item.height > 0)
			lp = new LayoutParams(item.width, item.height);
		lp.topMargin = rp(3);
		lp.bottomMargin = rp(3);
		lp.addRule(RelativeLayout.CENTER_IN_PARENT);
		return lp;
	}

	class AlphaAnimatorListener implements AnimationListener {
		private View view;
		private float targetAlpha;

		AlphaAnimatorListener(View view, float targetAlpha) {
			this.view = view;
			this.targetAlpha = targetAlpha;
		}

		public void setView(View view) {
			this.view = view;
		}

		public View getView() {
			return view;
		};

		@Override
		public void onAnimationEnd(Animation animation) {
			// TODO Auto-generated method stub
			if (targetAlpha == 0) {
				ViewUtils.runInMainThread(new Runnable() {

					@Override
					public void run() {
						// TODO Auto-generated method stub
						TitleBar.this.removeView(view);
					}
				});

			}
		}

		@Override
		public void onAnimationStart(Animation animation) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onAnimationRepeat(Animation animation) {
			// TODO Auto-generated method stub

		}

	}

	class PushTranslateAnimationListener implements AnimationListener {
		private View destView;
		private View preView;

		public PushTranslateAnimationListener(View preView, View destView) {
			this.destView = destView;
			this.preView = preView;
		}

		@Override
		public void onAnimationEnd(Animation animation) {
			// TODO Auto-generated method stub
			AlphaAnimation anim = new AlphaAnimation(0, 1);
			destView.setVisibility(View.VISIBLE);
			destView.clearAnimation();
			destView.startAnimation(anim);
			TitleBar.this.titleView = destView;
			TitleBar.this.leftButtonItem = preView;
		}

		@Override
		public void onAnimationRepeat(Animation animation) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onAnimationStart(Animation animation) {
			// TODO Auto-generated method stub

		}
	}

	class PopTranslateAnimationListener implements AnimationListener {
		private View destView;
		private View preView;

		public PopTranslateAnimationListener(View preView, View destView) {
			this.destView = destView;
			this.preView = preView;
		}

		@Override
		public void onAnimationEnd(Animation animation) {
			// TODO Auto-generated method stub
			AlphaAnimation anim = new AlphaAnimation(0, 1);
			destView.setVisibility(View.VISIBLE);
			destView.clearAnimation();
			destView.startAnimation(anim);
			TitleBar.this.leftButtonItem = destView;
			TitleBar.this.titleView = preView;
		}

		@Override
		public void onAnimationRepeat(Animation animation) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onAnimationStart(Animation animation) {
			// TODO Auto-generated method stub

		}
	}
}
